/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.create;

import br.com.deployment.daoImpl.CategoryDAOImpl;
import br.com.deployment.daoImpl.IssuesDAOImpl;
import br.com.deployment.daoImpl.OptionsDAOImpl;
import br.com.deployment.daoImpl.QuestionnaireDAOImpl;
import br.com.deployment.daoImpl.QuestionnaireResponseDAOImpl;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Options;
import br.com.deployment.entities.Questionnaire;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author roniere
 */
@ManagedBean
@SessionScoped
public class ManagerAnalyzeQuestionnaire implements Serializable {

    @EJB
    QuestionnaireResponseDAOImpl questionnaireResponseService;
    
    @EJB
    QuestionnaireDAOImpl questionnaireService;
    
    @EJB
    CategoryDAOImpl categoryService;
    
    @EJB
    IssuesDAOImpl issuesService;
    
    @EJB
    OptionsDAOImpl optionsService;
    
    private Questionnaire questionnaire;
    private List<Category> categories;
    private CartesianChartModel categoryModel;

    public ManagerAnalyzeQuestionnaire() {

        categoryModel = new CartesianChartModel();
        this.questionnaire = new Questionnaire();


    }

    @PostConstruct
    public void init() {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String action = params.get("action");
        if (action != null) {
            System.err.println(Long.parseLong(action) + "*");
            this.questionnaire = questionnaireService.find(1L);
            showIssues(questionnaire);
//            getCategory();
        }
    }
    
       // Visualiza o Questionário, Categorias, Perguntas ...
    public void showIssues(Questionnaire q) {
        System.err.println("SHOW Q");

        this.questionnaire = q;
        // Traz a Lista de Categorias do Questionário
        categories = categoryService.findCategoryQuestionnaire(this.questionnaire);
        for (Category c : categories) {
            // Traz Todas as Perguntas da Categoria
            List<Issues> issuesText = issuesService.findIssuesType(c, this.questionnaire);
            c.setIssueses(issuesText);

            for (Issues i : c.getIssueses()) {
                // Traz Todas as Opções da Pergunta
                List<Options> optionText = optionsService.findOptionsType(i);
                i.setOptions(optionText);

            }
        }
    }

    public void analyzeQuestionnaire(Questionnaire questionnaire) {
        this.questionnaire = questionnaire;
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect("analyzeQuestionnaire.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ManagerAnalyzeQuestionnaire.class.getName()).log(Level.SEVERE, null, ex);
        }
        getCategory();
    }

    public void getCategory() {
        categories = categoryService.findCategoryQuestionnaire(this.questionnaire);
        for (Category category : categories) {
            
            for (Issues issues : catchIssues(category)) {
                issues.setOptions(catchOptionses(issues));
                category.getIssueses().add(issues);
            }
            
        }
    }

    public List<Issues> catchIssues(Category c) {
        if (c != null) {
            return issuesService.findIssuesType(c, this.questionnaire);
        }
        return new ArrayList<Issues>();
    }

    public List<Options> catchOptionses(Issues i) {
        if (i != null) {
            return optionsService.findOptionsType(i);
        }
        return new ArrayList<Options>();
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public CartesianChartModel createGraphic(Issues issues) {
        categoryModel = new CartesianChartModel();

        ChartSeries cols;
        for (Options options : catchOptionses(issues)) {
            cols = new ChartSeries();
            cols.setLabel("" + (options.getTitle().length() > 15 ? options.getTitle().substring(0, 14).concat("...") :
                    options.getTitle()));
            Long countAmountOptionsSelected = optionsService.countAmountOptionsSelected(options);
            cols.set("", countAmountOptionsSelected);
            categoryModel.addSeries(cols);
        }
        
        return categoryModel;
    }

    public void teste(Category issues){
        System.err.println(issues);
    }
    public PieChartModel createPieModel(Issues issues) {

        PieChartModel pieModel = new PieChartModel();
        for (Options options : catchOptionses(issues)) {
            Long countAmountOptionsSelected = optionsService.countAmountOptionsSelected(options);
            pieModel.set(options.getTitle().length() > 15 ? options.getTitle().substring(0, 14).concat("...") :
                    options.getTitle(), countAmountOptionsSelected);
            System.err.println("Options"+ options.getId() + " issues "+issues.getId()+" - " + options.getTitle() + " " + countAmountOptionsSelected);
        }

        return pieModel;
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public void setCategoryModel(CartesianChartModel categoryModel) {
        this.categoryModel = categoryModel;
    }
}
